package star.cloud.test.person;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/person")
@Slf4j
public class PersonController {

    private static final List<Person> persons = new ArrayList<>();

    static {
        for (int i = 1; i < 100; i++) {
            persons.add(new Person(i, "yinhongqi" + i));
        }
    }

    @GetMapping
    public List<Person> getAll() {
        return persons;
    }

    @GetMapping("/welcome")
    public String welcome(Model model) {
        model.addAttribute("persons", persons);
        return "Welcome to person controller";
    }

    @GetMapping("/{id}")
    public Person getById(@PathVariable int id) {
        return persons.stream().filter(p -> p.getId() == id).findAny().orElse(null);
    }

    @PostMapping("/upload")
    public String upload(Person person, MultipartFile file) {
        System.out.println(person);
        if (file != null) {
            System.out.println(file.getOriginalFilename());
        }
        return "welcome to person controller";
    }

}
